package com.example.epidemic_assistant.ui.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.epidemic_assistant.gen.DaoSession;
import com.android.epidemic_assistant.gen.UserBeanDao;
import com.example.epidemic_assistant.App;
import com.example.epidemic_assistant.R;
import com.example.epidemic_assistant.bean.UserBean;
import com.example.epidemic_assistant.ui.DeviceManagerDetailsActivity;
import com.example.epidemic_assistant.ui.UserManagerDetailsActivity;
import com.example.epidemic_assistant.ui.adapter.DeviceManagerAdapter;
import com.example.epidemic_assistant.ui.adapter.UserManagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备管理
 */
public class DeviceManagerFragment extends Fragment implements DeviceManagerAdapter.IOnItemClick{

    private final static String TAG = "DeviceManagerFragment";

    private RecyclerView mRvDevice;

    private DeviceManagerAdapter mDeviceManagerAdapter;

    private App mMyApp;
    private DaoSession mDaoSession;
    private UserBeanDao mUserBeanDao;
    private List<UserBean> mUserBeans;

    private List<UserBean> mUserBeanList;
    private List<Integer> mRoomIdList;

    public static DeviceManagerFragment newInstance() {
        return new DeviceManagerFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_device_manager, container, false);

        initView(view);
        initData();
        initListener();

        return view;
    }

    private void initListener() {

    }

    private void initData() {
        mMyApp = (App) getActivity().getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mUserBeanDao = mDaoSession.getUserBeanDao();
        mUserBeans = mUserBeanDao.loadAll();

        mUserBeanList = new ArrayList<>();
        mRoomIdList = new ArrayList<>();
        for (UserBean userBean : mUserBeans) {
            if (!userBean.isAdmin() && !mRoomIdList.contains(userBean.getRoomId()) && userBean.getIsMaintain()) {
                mUserBeanList.add(userBean);
                mRoomIdList.add(userBean.getRoomId());
            }
        }

        mRvDevice.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        mDeviceManagerAdapter = new DeviceManagerAdapter(requireContext(), mUserBeanList);
        mDeviceManagerAdapter.setIOnItemClick(this);
        mRvDevice.addItemDecoration(new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL));
        mRvDevice.setAdapter(mDeviceManagerAdapter);
    }

    private void initView(View view) {
        mRvDevice = view.findViewById(R.id.rv_device);
    }

    @Override
    public void adapterMaintainClick(int position) {
        UserBean userBean = mUserBeanList.get(position);
        userBean.setIsMaintain(false);
        mUserBeanDao.update(userBean);
        mUserBeanList.remove(position);
        mDeviceManagerAdapter.notifyDataSetChanged();
    }

    @Override
    public void adapterItemClick(int position) {
        Intent intent = new Intent(requireContext(), DeviceManagerDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("position",position);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}